import uniPopup from '@/components/uni-popup/uni-popup.vue' // 弹出框组件

export default {
  components: {
    uniPopup, //弹出框组件
  },
  data() {
    return {
      btnnum: 0,
      faceimg: '', //正面
      backimg: '', //背面
      companyName: '',
      imgPolicy: '',
      imgAuthorization: '',
      imgHost: '',
      img1: null,
      img2: null,
      phone: '', //电话
    }
  },
  computed: {
    servicePhone() {
      return this.$store.state.user.servicePhone + ''
    },

    issub() {
      if (this.faceimg && this.backimg) {
        return true
      }
      return false
    },
  },

  onBackPress() {
    if (this.$store.state.user.userInfo.permious) {
      return false
    } else {
      uni.redirectTo({
        url: '/pages_login/register/register?phone=' + this.phone,
      })
      return true
    }
  },

  onLoad(e) {

    this.phone = e.phone?e.phone:this.$store.state.user.userInfo.mobile
    console.log(this.phone)
    if (e.companyName) {
      this.companyName = e.companyName
    } else {
      this.getUserCompany()
    }
    console.log(this.$store.state.user)
  },

  methods: {
    //获取注册/加入公司信息
    getUserCompany() {
      this.$api.user.getUserCompany().then(res => {
        if (res.data.name) {
          this.$store.commit('user/setCompanyInfo', {
            name: res.data.name,
            isJoinCompany: !res.data.isNew,
          })
          this.companyName = res.data.name
        }
      })
    },
    // 打电话
    callPhone() {
      uni.makePhoneCall({
        phoneNumber: this.servicePhone,
      })
    },

    //选择图片
    uploadImage(type) {
      const _this = this
      _this.img1 = null
      _this.img2 = null
      uni.showActionSheet({
        itemList: ['拍照', '打开相册'],
        success(res) {
          const cameraType = res.tapIndex ? 'album' : 'camera'
          uni.chooseImage({
            count: 1, // 默认9
            sizeType: ['original', 'compressed'], // 可以指定是原图还是压缩图，默认二者都有
            sourceType: [cameraType], // 从相册选择
            success(result) {
              uni.showLoading({
                title: '图片上传中~',
              })
              _this.$api.files
                .uploadImg({
                  filePath: result.tempFilePaths[0],
                })
                .then(res => {
                  if (type === 1) {
                    //法人身份证人像面
                    _this.faceimg = res.baseUrl + res.url
                  } else {
                    //法人身份证国徽面
                    _this.backimg = res.baseUrl + res.url
                  }
                  uni.hideLoading()
                })
                .catch(e => {
                  uni.hideLoading()
                  uni.showToast({
                    title: '图片上传失败，请稍后重试2',
                    icon: 'none',
                  })
                })
            },
          })
        },
      })
    },

    //提交认证信息
    async userIdentity() {
      if (!this.faceimg || !this.backimg) {
        uni.showToast({
          title: !this.faceimg
            ? '请上传身份证人像面照片'
            : '请上传身份证国徽面照片',
          icon: 'none',
        })
        return
      }
      uni.showLoading()
      try {
        const res = await this.$api.user.userIdentity({
          faceimg: this.faceimg,
          backimg: this.backimg,
          companyName: this.$store.state.user.isJoinCompany
            ? this.companyName
            : '',
        })
        uni.hideLoading()
        console.log(res, 1111, this.$store.state.user.userInfo)
        if (res.code === 200) {
          // 资料未完成
          if (this.$store.state.user.userInfo.infoDegree === 'IdentityInfoNone') {
            //邀请注册
            uni.showToast({
              title: '提交成功',
              duration: 2000,
            })
            setTimeout(() => {
              uni.reLaunch({
                url: '/pages_login/enterCompany/enterCompany?phone='+this.phone,
              })
            }, 2000)
          }else{
            // 是否有权限
            if (this.$store.state.user.userInfo.permious) {
						  //邀请注册
						  uni.showToast({
						    title: '提交成功',
						    duration: 2000,
						  })
						  setTimeout(() => {
						    uni.reLaunch({
						      url: '/pages_login/enterCompany/enterCompany?phone='+this.phone,
						    })
						  }, 2000)

            } else {
						  //主动注册
						  if (!this.$store.state.user.isJoinCompany) {
						         uni.redirectTo({
						           url: '/pages_login/authenticationCompany/authenticationCompany?companyName=' +
						    this.companyName+'&phone='+this.phone,
						         })
						  } else {
						    this.$refs.popup.open()
						    let joinData = {
						      mobile: this.phone,
						      companyName: this.companyName,
						    }
						    // 加入公司发送短信
						    this.$api.user.ApplyJoin(joinData).then(res => {
						      console.log(res)
						    }).catch(err => {
						      console.log(err)
						    })

						  }
            }
          }

        }

      } catch (e) {
        console.log(e, 2222)
        //TODO handle the exception
        this.img1 = e.msg.indexOf('人像')
        this.img2 = e.msg.indexOf('国徽')
        uni.hideLoading()
      }
    },

    //关闭弹窗
    closePopup() {
      this.$refs.popup.close()
      uni.navigateTo({
        url: '/pages_login/authenticationState/authenticationState?state=0&phone=' + this.phone+'&companyName='+this.companyName,
      })
    },
  },
}
